﻿#ifndef RAMS_H
#define RAMS_H

#include <QMainWindow>

#include "imu_tk/io_utils.h"
#include "imu_tk/calibration.h"
#include "imu_tk/filters.h"
#include "imu_tk/integration.h"
#include "qcustomplot.h"
#include <vector>
#include <QTime>
#include <QDebug>
#include "tcpclient.h"
#include "msg.h"
#include "imu.h"
#include <deque>

using namespace std;
using namespace imu_tk;
using namespace Eigen;

namespace Ui {
class RAMS;
}

class RAMS : public QMainWindow
{
    Q_OBJECT

public:
    explicit RAMS(QWidget *parent = nullptr);
    ~RAMS();

    friend void updata_acc(RAMS *rams, Eigen::Vector3d acc);

signals:
    void connectTcpServer(QString ip, int port);
    void disconnectTcpServer();
    void tcp_write(int);

private slots:
    void on_switchButton_Socket_checkedChanged(bool checked);

    void on_switchButton_data_checkedChanged(bool checked);

    void on_pushButton_load_clicked();// 从下位机载入校准数据

    void on_pushButton_save_clicked();// 保存校准文件到下位机

    void on_pushButton_data_save_clicked(); // 缓存数据保存

    void on_pushButton_data_clear_clicked();

    void on_pushButton_cali_clicked(); // 开始校准

    void on_pushButton_load_test_clicked();

    void on_pushButton_cal_axis_clicked(); // 计算旋转轴

    void on_pushButton_first_acc_clicked(); // 设定旋转起点

    void on_pushButton_start_clicked();

    void on_pushButton_sv_angle_clicked();

private:
    Ui::RAMS *ui;
    QTimer *dataTimer;
    TcpClient *tcpClient;
    QThread *clientThread;
    Msg *msg;
    IMU *imu;
    ZVD *zvd;
    std::vector< TriadData > acc_data;
    std::vector< TriadData > gyro_data;
    std::vector< TriadData > angle_data;

    std::vector<AngleData> angle_buf;
    std::deque<double> angle_mean_filter;
    bool show_angle = false;
    std::vector< TriadData >::size_type acc_data_it;
//    void get_acc_data_onlion(mavlink_message_t &msg);
//    void get_acc_cali_onlion(mavlink_message_t &msg);
     void plot_init();
    void plot_update();
    void get_data(QString s);
    void get_data();
    void calib_acc();
    void calib_acc(CalibratedTriad& calib,vector< TriadData > &acc,QPlainTextEdit& plainTextEdit );
    void plot_acc(QCustomPlot *qPlot);
    void plot_gyro(QCustomPlot *qPlot);
    void plot_angle(QCustomPlot *qPlot);
    bool is_get_calib = false;
};

inline void updata_acc(RAMS *rams, Eigen::Vector3d acc){
    rams->imu->update_data(acc);
}


#endif // RAMS_H
